package com.zking.ssm.sys.controller;

import com.alibaba.fastjson.JSONObject;
import com.zking.ssm.sys.model.HotelBrandType;
import com.zking.ssm.sys.service.IHotelBrandTypeService;
import com.zking.ssm.util.JsonData;
import com.zking.ssm.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/HotelBrandType")
public class HotelBrandTypeController {
    @Autowired
    private IHotelBrandTypeService Service;

    @ResponseBody
    @RequestMapping("/add")
    public Map<String, Object> merge(HotelBrandType hotelBrandType) {

        Map<String, Object> map = new HashMap<>();
        int insert = Service.insert(hotelBrandType);
        if (insert > 0) {
            map.put("code", 0);
            map.put("message", "增加成功");
        } else {
            map.put("code", -1);
            map.put("message", "增加失败");
        }

        return map;
    }

    @ResponseBody
    @RequestMapping("/del")
    public Map<String, Object> del(HotelBrandType hotelBrandType) {
        Map<String, Object> map = new HashMap<>();
        int insert = Service.deleteByPrimaryKey(hotelBrandType.getTypeId());
        if (insert > 0) {
            map.put("code", 0);
            map.put("message", "删除成功");
        } else {
            map.put("code", -1);
            map.put("message", "删除失败");
        }
        return map;
    }

    @RequestMapping("/de")
    public Map<String, Object> de(String id) {
        Map<String, Object> map = new HashMap<>();
        //把前端传过来的json字符串变成数组
        HotelBrandType[] sysUserIds = JSONObject.parseObject(id, HotelBrandType[].class);
        if (id != null) {
            for (HotelBrandType ids : sysUserIds) {
                Service.deleteByPrimaryKey(ids.getTypeId());
                map.put("message", "删除成功");
            }
        } else {
            map.put("message", "删除失败");
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/get/{id}")
    public JsonData get(@PathVariable Long id) {
        JsonData jsonData = new JsonData();
        System.out.println(id);

        HotelBrandType a = Service.selectByPrimaryKey(id);

        jsonData.setCode(0);
        jsonData.setMessage("查询成功");
        jsonData.setResult(a);
        return jsonData;
    }

    @ResponseBody
    @RequestMapping("/list")
    public Map<String, Object> list(HotelBrandType hotelBrandType, PageBean pageBean, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        pageBean.setRequest(request);
        pageBean.setPage(pageBean.getPage());
        pageBean.setRows(pageBean.getRows());
        pageBean.setTotal(pageBean.getTotal());
        List<HotelBrandType> userList = Service.list(hotelBrandType, pageBean);
        map.put("code", 0);
        map.put("result", userList);
        map.put("page", pageBean.getPage());
        map.put("rows", pageBean.getRows());
        map.put("total", pageBean.getTotal());
        return map;
    }

    @RequestMapping("/up")
    public Map<String, Object> up(HotelBrandType hotelBrandType) {
        Map<String, Object> map = new HashMap<String, Object>();
        int insert = Service.updateByPrimaryKey(hotelBrandType);
        if (insert > 0) {
            map.put("code", 0);
            map.put("message", "修改成功");
        } else {
            map.put("code", -1);
            map.put("message", "修改失败");
        }
        return map;
    }

}
